package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.CategoryDao;
import com.kang.pojo.Category;
import com.kang.service.CategoryService;
import com.vonechina.entity.page.CategoryPage;

/**
 * 物品分类服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class CategoryServiceImpl implements CategoryService {
	/**
	 * 物品分类持久层
	 */
	@Resource
	private CategoryDao dao;

	/**
	 * 删除物品分类
	 * 
	 * @param categoryid
	 *            物品分类ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer categoryid) {

		return dao.deleteByPrimaryKey(categoryid);
	}

	/**
	 * 新增物品分类
	 * 
	 * @param record
	 *            物品分类实体类
	 * @return
	 */
	@Override
	public int insertSelective(Category record) {

		return dao.insertSelective(record);
	}

	/**
	 * 通过主键查询物品分类
	 * 
	 * @param categoryid
	 *            物品分类ID
	 * @return
	 */
	@Override
	public Category selectByPrimaryKey(Integer categoryid) {

		return dao.selectByPrimaryKey(categoryid);
	}

	/**
	 * 更新物品分类
	 * 
	 * @param record
	 *            物品分类实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(Category record) {

		return dao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 分页查询物品分类
	 * 
	 * @param categoryPage
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<Category> listCategory(CategoryPage categoryPage) {
		categoryPage.setRows(dao.countCategory(categoryPage));
		return dao.listCategory(categoryPage);
	}

	/**
	 * 删除下级物品分类
	 * 
	 * @param categoryid
	 *            物品分类ID
	 * @return
	 */
	@Override
	public int deletenext(Integer categoryid) {
		return dao.deletenext(categoryid);
	}

	/**
	 * 查询下一级物品分类
	 * 
	 * @param categoryid
	 *            物品分类ID
	 * @return
	 */
	@Override
	public List<Category> selectid(Integer categoryid) {
		return dao.selectid(categoryid);
	}

	/**
	 * 查询全部物品分类
	 * 
	 * @return
	 */
	@Override
	public List<Category> listAllCategory() {
		List<Category> list1 = dao.selectid(0);
		List<Category> result = new ArrayList<>();
		for (Category c1 : list1) {
			result.add(c1);
			List<Category> list2 = dao.selectid(c1.getCategoryid());
			for (Category c2 : list2) {
				c2.setCategoryname("&nbsp;&nbsp;" + c2.getCategoryname());
				result.add(c2);
				List<Category> list3 = dao.selectid(c2.getCategoryid());
				for (Category c3 : list3) {
					c3.setCategoryname("&nbsp;&nbsp;&nbsp;&nbsp;" + c3.getCategoryname());
					result.add(c3);
				}
			}
		}
		return result;
	}

	/**
	 * 生成物品分类编号
	 * 
	 * @return
	 */
	@Override
	public String generateCategoryCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = dao.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "LB" + no;
	}

}
